from pydantic import BaseModel, Field
from typing import Optional, List
from datetime import datetime

class CategoryBase(BaseModel):
    """分类基础Schema"""
    name: str = Field(..., min_length=1, max_length=100)
    description: Optional[str] = None
    icon: Optional[str] = None
    color: Optional[str] = None
    parent_id: Optional[str] = None
    sort_order: int = 0

class CategoryCreate(CategoryBase):
    """创建分类Schema"""
    pass

class CategoryUpdate(BaseModel):
    """更新分类Schema"""
    name: Optional[str] = Field(None, min_length=1, max_length=100)
    description: Optional[str] = None
    icon: Optional[str] = None
    color: Optional[str] = None
    parent_id: Optional[str] = None
    sort_order: Optional[int] = None

class CategoryResponse(CategoryBase):
    """分类响应Schema"""
    id: str
    created_by: Optional[str]
    created_at: datetime
    updated_at: Optional[datetime]
    
    # 统计信息（可选）
    document_count: int = 0
    children: List['CategoryResponse'] = []
    
    class Config:
        from_attributes = True

class CategoryListResponse(BaseModel):
    """分类列表响应Schema"""
    items: List[CategoryResponse]
    total: int

# 用于树形结构
CategoryResponse.model_rebuild()
